package com.egg.ylt.fragment;

import android.net.Uri;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.activity.ACT_SearchNews;
import com.egg.ylt.adapter.ADA_RecommendShop;
import com.egg.ylt.adapter.ADA_TimesCard;
import com.egg.ylt.controls.DIALOG_HomepageCoupon;
import com.egg.ylt.pojo.BannerDataEntity;
import com.egg.ylt.pojo.BannerEntity;
import com.egg.ylt.pojo.CardDataEntity;
import com.egg.ylt.pojo.CouponEntity;
import com.egg.ylt.pojo.NatatoriumListPageEntity;
import com.egg.ylt.presenter.impl.HomePresenterImpl;
import com.egg.ylt.view.IHomeView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.ArrowRefreshHeader;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.umeng.commonsdk.debug.I;
import com.yonyou.framework.library.base.BaseFragment;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.common.utils.ListUtil;
import com.yonyou.framework.library.common.utils.ScreenUtil;
import com.yonyou.framework.library.eventbus.EventCenter;
import com.yonyou.framework.library.widgets.xbanner.XBanner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by lijiateng on 2019/1/16.
 */

public class FRA_Home extends BaseFragment<HomePresenterImpl> implements IHomeView {

    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.rv_home)
    XRecyclerView rvHome;

    private XBanner mBanner;
    private RecyclerView mRvCard;

    private ADA_TimesCard cardAdapter;
    private ADA_RecommendShop shopAdapter;

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    protected void onFirstUserVisible() {

    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            mPresenter.getHomeData();
        }
    }

    @Override
    protected View getLoadingTargetView() {
        return rvHome;
    }

    @Override
    protected void initViewsAndEvents() {

        //动态设置沉浸式状态栏的高度
        int statusBarHeight = ScreenUtil.getStatusBarHeight(mContext);
        llSearch.setPadding(ScreenUtil.dip2px(mContext, 10), statusBarHeight,
                ScreenUtil.dip2px(mContext, 10), llSearch.getPaddingBottom());

        // 初始化 banner 列表
        initHomeBanner();

        // 初始化 次卡 列表
        initHomeCard();

        // 初始化 商家 列表
        initHomeShopList();

        mPresenter.getHomeData();

        // 优惠券弹窗
        if (mPresenter.isUserLogin()) {
            mPresenter.getCouponInfo();
        }

    }

    /**
     * 商家列表
     */
    private void initHomeShopList() {
        shopAdapter = new ADA_RecommendShop(mContext);
        LinearLayoutManager shopLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rvHome.setLayoutManager(shopLayoutManager);
        rvHome.setAdapter(shopAdapter);

        rvHome.setPullRefreshEnabled(false);
        rvHome.setLoadingMoreEnabled(false);

    }

    /**
     * 次卡列表
     */
    private void initHomeCard() {
        LinearLayout cardLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.fra_home_card, null);
        LinearLayout.LayoutParams cardLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        cardLayoutParams.setMargins(ScreenUtil.dip2px(mContext, 10), 0,
                ScreenUtil.dip2px(mContext, 10), ScreenUtil.dip2px(mContext, 10));
        cardLayout.setLayoutParams(cardLayoutParams);

        mRvCard = cardLayout.findViewById(R.id.rv_times_card);
        cardAdapter = new ADA_TimesCard(mContext);
        mRvCard.setAdapter(cardAdapter);
        LinearLayoutManager cardLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        mRvCard.setLayoutManager(cardLayoutManager);

        rvHome.addHeaderView(cardLayout);
    }

    /**
     * 轮播图
     */
    private void initHomeBanner() {
        FrameLayout bannerLayout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.fra_home_banner, null);
//        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

//        layoutParams.setMargins(ScreenUtil.dip2px(mContext, 5), 0,
//                ScreenUtil.dip2px(mContext, 5), 0);
//        bannerLayout.setLayoutParams(layoutParams);

//        bannerLayout.setPadding(ScreenUtil.dip2px(mContext, 5), 0,
//                ScreenUtil.dip2px(mContext, 5), 0);

        mBanner = bannerLayout.findViewById(R.id.banner);
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mBanner.getLayoutParams();
        params.height = ScreenUtil.getScreenWidth(getActivity()) / 2;
        mBanner.setLayoutParams(params);

        //加载广告图片
        mBanner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                //此处适用Fresco加载图片，可自行替换自己的图片加载框架

                SimpleDraweeView draweeView = (SimpleDraweeView) view;
                BannerEntity listBean = ((BannerEntity) model);
                draweeView.setImageURI(Uri.parse(listBean.getUrl()));

            }
        });
        rvHome.addHeaderView(bannerLayout);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fra_home_new;
    }

    @Override
    protected void onEventComming(EventCenter eventCenter) {
        switch (eventCenter.getEventCode()) {
            case Constants.REQUEST_HOMEPAGE_COUPON:
                mPresenter.getCouponInfo();
                break;
        }
    }

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }


    @Override
    public void updateHomeBanner(BannerDataEntity result) {

        ArrayList<BannerEntity> arrayList = new ArrayList<>();
        for (int i = 0; i < result.getBannerList().size(); i++) {
            BannerEntity bannerEntity = new BannerEntity();
            bannerEntity.setUrl(result.getBannerList().get(i));
            arrayList.add(bannerEntity);
        }

        mBanner.setBannerData(R.layout.layout_fresco_imageview, arrayList);

    }

    @Override
    public void updateCardList(List<CardDataEntity.CardListEntity> cardList) {

        if (ListUtil.isListEmpty(cardList)) {

            int count = rvHome.getHeaders_includingRefreshCount();
            for (int i = 0; i <= count; i++) {
                View view = rvHome.getChildAt(i);
                if ((!(view instanceof ArrowRefreshHeader) && view instanceof LinearLayout)) {

                    LinearLayout llCard = rvHome.findViewById(R.id.ll_times_card);
                    RecyclerView rvCard = llCard.findViewById(R.id.rv_times_card);
                    LinearLayout emptyCard = llCard.findViewById(R.id.ll_card_empty);
                    rvCard.setVisibility(View.GONE);
                    emptyCard.setVisibility(View.VISIBLE);

                    break;
                }
            }
            return;
        }
        cardAdapter.update(cardList, true);
    }

    @Override
    public void updateShopList(List<NatatoriumListPageEntity.ListBean> list) {
        shopAdapter.update(list, true);
    }

    @Override
    public void showCouponDialog(CouponEntity entity) {
        final DIALOG_HomepageCoupon dialog = new DIALOG_HomepageCoupon(getActivity(), entity);
        dialog.show();
    }

    @OnClick({R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_search:
                readyGo(ACT_SearchNews.class);
                break;
        }
    }

}
